﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;

namespace Kotonoha.Server.Utils {
    public class ObservableList<T> : IList<T>, INotifyCollectionChanged , IList
        where T : class, INotifyPropertyChanged {
        private void InvokeCollectionChanged(NotifyCollectionChangedEventArgs e) {
            NotifyCollectionChangedEventHandler handler = CollectionChanged;
            if (handler != null) handler(this, e);
        }

        public event NotifyCollectionChangedEventHandler CollectionChanged;
        
        private readonly IList<T> list;

        public ObservableList(IList<T> list) {
            this.list = list;
        }

        public T this[int idx] {
            get { return list[idx]; }
            set {
                T old = list[idx];
                list[idx] = value;
                InvokeCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, value, old, idx));
            }
        }

        public IEnumerator<T> GetEnumerator() {
            return list.GetEnumerator();
        }

        public void Add(T item) {
            list.Add(item);
            InvokeCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
        }

        public void Clear() {
            list.Clear();
            InvokeCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public bool Contains(T item) {
            return list.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex) {
            list.CopyTo(array, arrayIndex);
        }

        public bool Remove(T item) {
            bool removed = list.Remove(item);
            if (removed) {
                InvokeCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
            }
            return removed;
        }

        public int Count {
            get { return list.Count; }
        }

        public bool IsReadOnly {
            get { return list.IsReadOnly; }
        }

        public int IndexOf(T item) {
            return list.IndexOf(item);
        }

        public void Insert(int index, T item) {
            list.Insert(index, item);
            InvokeCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index));
        }

        public void RemoveAt(int index) {
            T item = null;
            if (0 <= index && index < Count) {
                item = list[index];
            }
            list.RemoveAt(index);
            if (item != null) {
                InvokeCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index));
            }
        }

        IEnumerator IEnumerable.GetEnumerator() {
            return GetEnumerator();
        }

        void ICollection.CopyTo(Array array, int index) {
            this.CopyTo((T[])array, index);
        }

        int ICollection.Count {
            get { return Count; }
        }

        public object SyncRoot {
            get { return null; }
        }

        public bool IsSynchronized {
            get { throw new NotImplementedException(); }
        }

        public int Add(object value) {
            this.Add((T)value);
            return this.Count - 1;
        }

        public bool Contains(object value) {
            return this.Contains((T)value);
        }


        public int IndexOf(object value) {
            return this.IndexOf((T)value);
        }

        public void Insert(int index, object value) {
            this.Insert(index, (T)value);
        }

        public void Remove(object value) {
            this.Remove((T)value);
        }


        object IList.this[int index] {
            get { return this[index]; }
            set { this[index] = (T)value; }
        }


        public bool IsFixedSize {
            get { return false; }
        }
    }
}